<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org/">
<head>
    <meta charset="UTF-8">
    <title>博客修改</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" th:href="@{/layuimini/lib/layui-v2.6.3/css/layui.css}" media="all">
    <link rel="stylesheet" th:href="@{/layuimini/css/public.css}" media="all">
    <link rel="stylesheet" th:href="@{/layuimini/lib/editormd/css/editormd.min.css}">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>

<div class="layui-form layuimini-form">
    <input type="hidden" name="id" id="id" th:value="${blog.id}">
    <div class="layui-form-item">
        <label class="layui-form-label required">标题</label>
        <div class="layui-col-md9 layui-show-xs-block">
            <input type="text" name="title" th:value="${blog.title}" placeholder="请输入文章标题" autocomplete="off" class="layui-input" lay-verify="required">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label required">介绍</label>
        <div class="layui-col-md9 layui-show-xs-block">
            <input type="text" name="description" th:value="${blog.description}" lay-verify="required|description" placeholder="请输入文章介绍(100字以内)" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label required">封面</label>
        <div class="layui-upload">
            <button type="button" class="layui-btn" id="selectImg">选择图片</button>
            <button type="button" class="layui-btn" id="uploadImg">上传图片</button>
            <input id="cover" th:src="${blog.cover}" type="hidden" name="cover" lay-verify="required" lay-reqtext="请选择好封面然后点击上传图片" class="layui-input">
            <div class="layui-upload-list">
                <img style="width: 190px; height: 90px; margin-left: 12.5%" id="pic">
            </div>
            <div style="width: 190px; margin-left: 12.5%">
                <div class="layui-progress layui-progress-big" lay-showpercent="yes" lay-filter="progress">
                    <div class="layui-progress-bar" lay-percent=""></div>
                </div>
            </div>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label required">分类</label>
        <div class="layui-col-md3 layui-show-xs-block">
            <div id="typeId" class="xm-select-demo"></div>
        </div>

        <label class="layui-form-label required">标签</label>
        <div class="layui-col-md4 layui-show-xs-block">
            <div id="tagIds" class="xm-select-demo"></div>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">是否开启</label>
        <div class="layui-col-md10 layui-show-xs-block layui">
            <input type="checkbox" th:checked="${blog.flag}" name="flag" lay-skin="switch" value="1" lay-text="原创|转载">
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <input type="checkbox" th:checked="${blog.published}" name="published" lay-skin="switch" value="1" lay-text="发布|草稿">
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <input type="checkbox" th:checked="${blog.commentable}" name="commentable" value="1" lay-skin="switch" lay-text="允许评论|不允许评论">
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <input type="checkbox" th:checked="${blog.appreciate}" name="appreciate" value="1" lay-skin="switch" lay-text="打赏|免费">
        </div>
    </div>

    <div class="layui-form-item">
        <div id="md-content" style="z-index: 1 !important;">
            <textarea name="content" th:text="${blog.content}" placeholder="博客内容" style="display: none"></textarea>
        </div>
    </div>

    <div class="layui-input-block">
        <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">修改博客</button>
    </div>
</div>

<script th:src="@{/layuimini/lib/layui-v2.6.3/layui.js}" charset="utf-8"></script>
<script th:src="@{/layuimini/lib/jquery-3.4.1/jquery-3.4.1.min.js}"></script>
<script th:src="@{/layuimini/js/lay-config.js}" charset="utf-8"></script>
<script th:src="@{/layuimini/lib/editormd/editormd.min.js}"></script>
<script>
    layui.use(["jquery", "form", "layer", "xmSelect"], function () {
        var $ = layui.jquery,
            layer = layui.layer,
            form = layui.form,
            xmSelect = layui.xmSelect,
            element = layui.element,
            upload = layui.upload;

        upload.render({
            elem: '#selectImg',
            url: '/admin/upload/uploadBlogCover', //此处用的是第三方的 http 请求演示，实际使用时改成您自己的上传接口即可。
            auto: false,
            bindAction: '#uploadImg',
            choose: function(obj){
                //预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result){
                    $('#pic').attr('src', result); //图片链接（base64）
                });
            },
            done: function(res){
                //上传成功的一些操作
                if (res.code === 200) {
                    $('#cover').val(res.data.url)
                } else if (res.code === 444) {
                    $('#picText').html(''); //置空上传失败的状态

                }
            },
            //进度条
            progress: function(n, elem, e){
                element.progress('progress', n + '%'); //可配合 layui 进度条元素使用
                if(n == 100){
                    layer.msg('上传完毕', {icon: 1});
                }
            }
        });

        //分类列表
        $.post('/admin/type/selectAll', function (res) {
            var typeData = res.data.types
            var typeSelect = xmSelect.render({
                el: '#typeId',
                tips: '请选择分类',
                name: 'typeId',
                radio: true,
                clickClose: true,
                model: {
                    label: {
                        type: 'text',
                        text: {
                            //左边拼接的字符
                            left: '',
                            //右边拼接的字符
                            right: '',
                            //中间的分隔符
                            separator: ', ',
                        },
                    }
                },
                layVerify: 'required',
                layReqText: '请选择分类',
                data: typeData
            })
            var blogId = $('#id').val();
            $.get('/admin/type/selectChecked/' + blogId, function (res) {
                typeSelect.setValue(res.data.typeSelect)
            })
        })

        //标签列表
        var tagData
        $.post('/admin/tag/selectAll', function (res) {
            tagData = res.data.tags
            var tagSelect = xmSelect.render({
                el: '#tagIds',
                name: 'tagIds',
                tips: '请选择标签',
                toolbar: {
                    show: true,
                    list: [ 'ALL', 'CLEAR', 'REVERSE' ]
                },
                layVerify: 'required',
                layReqText: '请选择标签',
                data: tagData
            })
            var blogId = $('#id').val();
            $.get('/admin/tag/selectChecked/' + blogId, function (res) {
                console.log(res.data.tagSelect)
                tagSelect.setValue(res.data.tagSelect)
            })
        })

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            data = data.field

            $.post('/admin/blog/editBlog', data, function (res) {
                if (res.code === 200) {
                    layer.msg(res.msg, function () {
                        var iframeIndex = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(iframeIndex);
                        //刷新父页面
                        window.parent.location.reload();
                    })
                } else if (res.code === 444) {
                    layer.msg(res.msg)
                }
            })

            return false;
        });

        //初始化markdown编译器
        var contentEditor;
        $(function() {
            contentEditor = editormd("md-content", {
                width   : "75%",
                height  : 600,
                align   : "center",
                syncScrolling : "single",
                path    : "/layuimini/lib/editormd/lib/",
                toolbarModes: 'full',
            });
        });

        form.verify({
            description: function (value, item) { //value：表单的值、item：表单的DOM对象
                console.log(value.length);
                if (value.length > 100) {
                    return '介绍不能超过100个字符';
                }
            }
        });

    });

</script>
</body>
</html>
